package com.aramis.mydytt.ui.base

import com.aramis.mydytt.api.DyttApi
import com.aramis.mydytt.api.MovieApi
import com.aramis.mydytt.ext.md5
import okhttp3.OkHttpClient
import okhttp3.Request
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.*


/**
 * Description: Net
 * @author lizhidan
 * Created: 2021/4/7
 */
object Net {
    private const val BASE_URL = "http://api2.rinhome.com"
    private const val BASE_URL_DYTT = "http://m.dydytt.net:8080"
    var movieApi: MovieApi? = null
        get() {
            if (field == null) {
                val retrofit = Retrofit.Builder().baseUrl(BASE_URL)
                    .client(getOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                    .callbackExecutor(Executors.newSingleThreadExecutor())
                    .build()
                field = retrofit.create(MovieApi::class.java)
            }
            return field
        }
        private set

    private fun getOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder()
            .addInterceptor { chain ->
                val request: Request = chain.request()
                    .newBuilder()
                    .removeHeader("User-Agent") //移除旧的
                    .addHeader("User-Agent", "jianpian-android/332") //添加真正的头部
                    .addHeader("JPAUTH", "che8PySPSAUdYYRUCMUGpb0r3aWZo5SkhYJf5N969D2g")
                    .build()
                chain.proceed(request)
            }.build()
    }

    var dyttApi: DyttApi? = null
        get() {
            if (field == null) {
                val retrofit = Retrofit.Builder().baseUrl(BASE_URL_DYTT)
                    .client(getDyttOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build()
                field = retrofit.create(DyttApi::class.java)
            }
            return field
        }
        private set

    private fun getDyttOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder()
            .addInterceptor { chain ->
                val timestamp=System.currentTimeMillis()/1000
                val request: Request = chain.request()
                    .newBuilder()
                    .removeHeader("User-Agent") //移除旧的
                    .addHeader("User-Agent", "okhttp/3.8.0") //添加真正的头部
                    .addHeader("x-header-request-imei", "")
                    .addHeader("x-header-request-key", getDyttKey(timestamp))
                    .addHeader("x-header-request-timestamp", timestamp.toString())
                    .build()
                chain.proceed(request)
            }.build()
    }

    private fun getDyttKey(timestamp:Long): String {
        return ("wq0LQbLUTH66x-header-request-timestamp="+timestamp+"x-header-request-imei=").md5().toLowerCase(Locale.getDefault())
    }
}